package com.campus.common.vo;

import java.io.Serializable;

public class Book implements Serializable {
    private int id;            // 书籍ID
    private String title;       // 书名
    private String author;      // 作者
    private boolean available; // 是否可用
    private int stock;         // 库存数量
    private int timeLimit;     // 可借天数（新增字段）
    private String category;
    // 无参构造函数
    public Book() {}

    // 全参构造函数
    public Book(int id, String title, String author, boolean available, int stock, int timeLimit, String category) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.available = available;
        this.stock = stock;
        this.timeLimit = timeLimit;
        this.category = category;
    }

    // Getter和Setter方法
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public String getAuthor() { return author; }
    public void setAuthor(String author) { this.author = author; }

    public boolean isAvailable() { return available; }
    public void setAvailable(boolean available) { this.available = available; }

    public int getStock() { return stock; }
    public void setStock(int stock) { this.stock = stock; }

    public int getTimeLimit() { return timeLimit; }  // 新增Getter
    public void setTimeLimit(int timeLimit) { this.timeLimit = timeLimit; }  // 新增Setter

    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }
    // toString() 方法
    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", available=" + available +
                ", stock=" + stock +
                ", timeLimit=" + timeLimit +
                '}';
    }

    // equals() 和 hashCode() 方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id == book.id;
    }

    @Override
    public int hashCode() {
        return java.util.Objects.hash(id);
    }

    // 检查书籍是否可借
    public boolean canBorrow() {
        return available && stock > 0;
    }

    // 减少库存方法
    public void decreaseStock() {
        if (stock <= 0) {
            throw new IllegalStateException("库存不足");
        }
        stock--;
        if (stock == 0) {
            available = false;
        }
    }

    // 增加库存方法
    public void increaseStock() {
        stock++;
        if (!available) {
            available = true;
        }
    }
}